package proc

import (
	"encoding/json"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"github.com/hu17889/go_spider/core/common/page"
	corerequest "github.com/hu17889/go_spider/core/common/request"
	"github.com/hu17889/go_spider/core/pipeline"
	"github.com/hu17889/go_spider/core/spider"
	"net/http"
	"regexp"
	"strings"
	"tesou.io/platform/poem-parent/poem-api/common/base"
	"tesou.io/platform/poem-parent/poem-api/module/core/enums"
	"tesou.io/platform/poem-parent/poem-api/module/core/pojo"
	"tesou.io/platform/poem-parent/poem-core/module/core/service"
)

type ZhihuProcesser struct {
	zhihuService    service.ZhihuService
	SpiderUrl string
	cookies []*http.Cookie
}

func GetZhihuProcesser() *ZhihuProcesser {
	return &ZhihuProcesser{}
}

func (this *ZhihuProcesser) Startup() {
	newSpider := spider.NewSpider(this, "ZhihuProcesser")

	//newSpider = newSpider.AddUrl(this.SpiderUrl, "html")
	base.Log.Info("start URL:,", this.SpiderUrl)
	//hder := make(http.Header)
	//hder.Set("Cookie", `_zap=09ee8132-fd2b-43d3-9562-9d53a41a4ef5; d_c0="AGDv-acVoQ-PTvS01pG8OiR9v_9niR11ukg=|1561288241"; capsion_ticket="2|1:0|10:1561288248|14:capsion_ticket|44:NjE1ZTMxMjcxYjlhNGJkMjk5OGU4NTRlNDdkZTJhNzk=|7aefc35b3dfd27b74a087dd1d15e7a6bb9bf5c6cdbe8471bc20008feb67e7a9f"; z_c0="2|1:0|10:1561288250|4:z_c0|92:Mi4xeGZsekFBQUFBQUFBWU9fNXB4V2hEeVlBQUFCZ0FsVk5PcXo4WFFBNWFFRnhYX2h0ZFZpWTQ5T3dDMGh5ZTV1bjB3|0cee5ae41ff7053a1e39d96df2450077d37cc9924b337584cf006028b0a02f30"; q_c1=ae65e92b2bbf49e58dee5b2b29e1ffb3|1561288383000|1561288383000; tgw_l7_route=f2979fdd289e2265b2f12e4f4a478330; _xsrf=f8139fd6-b026-4f01-b860-fe219aa63543; tst=h; tshl=`)
	//hder.Set("User-Agent", `Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Mobile Safari/537.36`)
	//req := corerequest.NewRequest(this.SpiderUrl, "html", "hotindex", "GET", "", hder, nil, nil,nil)
	//newSpider.AddRequest(req)


	req := corerequest.NewRequestWithHeaderFile(this.SpiderUrl, "html",  "zhihu.com.json")
	newSpider.AddRequest(req)

	newSpider = newSpider.AddPipeline(pipeline.NewPipelineConsole())
	newSpider.SetThreadnum(2).Run()
}

func (this *ZhihuProcesser) Process(p *page.Page) {
	request := p.GetRequest()
	if !p.IsSucc() {
		base.Log.Info("URL:,", request.Url, p.Errormsg())
		return
	}
	//var allData []map[string]interface{}
	zhi_list_slice := make([]interface{}, 0)
	zhi_list_update_slice := make([]interface{}, 0)
	level := enums.GetListLevel("ZhiHu")
	p.GetHtmlParser().Find("body").Each(func(i int, selection *goquery.Selection) {
		aaa:= selection.Text()
		re := regexp.MustCompile(`<(.|\n)*?>`)
		aaa =  re.ReplaceAllString(aaa,"")   //hotList
		aaaword := strings.Split(aaa, "\"hotList\":")[1]  //, "guestFeeds"
		bbbword := strings.Split(aaaword, "guestFeeds")[0]
		bbbword = strings.TrimRight(bbbword, ",\"")
		cccword :=  strings.Replace(bbbword, "\\u002F", "/", -1)

		var mobile MobileInfo
		err := json.Unmarshal([]byte(cccword), &mobile)
		if err != nil {
			fmt.Println(err.Error())
		}

		for index, v := range mobile {
			fmt.Println(index)
			fmt.Println(v.Type)
			fmt.Println(v.StyleType)
			fmt.Println(v.FeedSpecific.AnswerCount)
			zhi := new(pojo.List)
			zhi.ListId = strings.TrimLeft(v.CardID,"Q_")
			zhi.Title = strings.TrimSpace(v.Target.TitleArea.Text)
			zhi.SUrl = strings.TrimSpace(v.Target.Link.URL)
			zhi.Image = strings.TrimSpace(v.Target.ImageArea.URL)
			zhi.Content = strings.TrimSpace(v.Target.ExcerptArea.Text)
			if len(zhi.Title) > 0 {
				//设置朝代
				zhi.Type = level
				exist := this.zhihuService.Exist(zhi)
				if !exist {
					zhi_list_slice = append(zhi_list_slice, zhi)
				} else {
					zhi_list_update_slice = append(zhi_list_update_slice, zhi)
				}
			}
		}

	})
	//执行入库
	this.zhihuService.SaveList(zhi_list_slice)
	this.zhihuService.ModifyList(zhi_list_update_slice)

}

func (this *ZhihuProcesser) Finish() {
	base.Log.Info("zhihu表抓取解析完成 \r\n")

}


type MobileInfo []struct {
	Type         string `json:"type"`
	StyleType    string `json:"styleType"`
	ID           string `json:"id"`
	CardID       string `json:"cardId"`
	FeedSpecific struct {
		AnswerCount int `json:"answerCount"`
	} `json:"feedSpecific"`
	Target struct {
		TitleArea struct {
			Text string `json:"text"`
		} `json:"titleArea"`
		ExcerptArea struct {
			Text string `json:"text"`
		} `json:"excerptArea"`
		ImageArea struct {
			URL string `json:"url"`
		} `json:"imageArea"`
		MetricsArea struct {
			Text string `json:"text"`
		} `json:"metricsArea"`
		LabelArea struct {
			Type        string `json:"type"`
			Trend       int    `json:"trend"`
			NightColor  string `json:"nightColor"`
			NormalColor string `json:"normalColor"`
		} `json:"labelArea"`
		Link struct {
			URL string `json:"url"`
		} `json:"link"`
	} `json:"target"`
	AttachedInfo string `json:"attachedInfo"`
}